package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 要求用户输入用户名，然后将该用户从user表中删除
 *
 * 预编译SQL
 */
public class DeleteUser {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个用户名:");
        String username = scanner.nextLine();

        try (Connection connection = DBUtil.getConnection();){
            String sql = "DELETE FROM user WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "删除成功" : "删除失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
